<?php
$loader->requireOnce('includes/AbsReportTemplate.class.php');
$loader->requireOnce('includes/paymentLogging/PaymentLogger.class.php');

/**
 * Daily revenue
 * @author ptnguyen
 *
 */
class Report01 extends AbsReportTemplate {
    
    private $collectingReceiptList;
    private $totalCollecting    = 0;
        
    private $paybackReceiptList;
    private $totalPayback       = 0;
    private $total              = 0;
    
    function __construct($name) {
        parent::__construct($name);
    }
    
    /**
     * (non-PHPdoc)
     * @see modules/report/local/includes/AbsReportTemplate#buildReportModel()
     */
    function buildReportModel() {
        $dateFormat =   L10nHelper::getSqlDateFormat("YmdHi");
        // Get all receipts of collecting type
        $collectingType =   EnumerationConstant::PAYMENT_COLLECTION_TYPE_COLLECTING_KEY;
        $start  =   $this->startDate . " 00:00";
        $end    =   $this->endDate . " 23:59";
        $sql    =   array(
            'cols'  =>  "pa.patient_code, pe.name patient_name, pr.receipt_code, pr.referenced_receipt, pr.amount,
                        DATE_FORMAT(pr.created_time, '$dateFormat') created_time_formatted, pr.cashier_name",
            'from'  =>  "pay_receipt pr
                        INNER JOIN patient pa ON (pr.patient_id = pa.person_id)
                        INNER JOIN person pe ON (pe.person_id = pa.person_id)",
            'where' =>  "pr.created_time >= '$start'
                AND pr.created_time <= '$end'
                AND pr.enum_payment_type = '$collectingType'"
        );
        $ds =   new Datasource_sql();
        $ds->setup($this->_db, $sql);
        $res    =   $ds->toArray();
        foreach ($res as &$item) {
            $this->totalCollecting  +=  $item['amount'];
            $item['amount'] =   L10nHelper::renderNumber($item['amount']);
        }
        $this->collectingReceiptList    =   $res;
        unset($res);
        unset($ds);
        
        /*$paybackType    =   EnumerationConstant::PAYMENT_COLLECTION_TYPE_PAYBACK_KEY;
        $sql    =   array(
            'cols'  =>  'pa.patient_code, pe.name patient_name, pr.receipt_code, pr.referenced_receipt, -pr.amount amount',
            'from'  =>  "pay_receipt pr
                        INNER JOIN patient pa ON (pr.patient_id = pa.person_id)
                        INNER JOIN person pe ON (pe.person_id = pa.person_id)",
            'where' =>  "'$start'<= pr.created_time AND pr.created_time <= '$end' AND pr.enum_payment_type = '$paybackType'"
        );
        $ds =   new Datasource_sql();
        $ds->setup($this->_db, $sql);
        $res    =   $ds->toArray();
        foreach ($res as &$item) {
            $this->totalPayback +=  $item['amount'];
            $item['amount'] =   L10nHelper::renderNumber($item['amount']);
        }
        $this->paybackReceiptList   =   $res;
        unset($res);
        unset($ds);*/

        // Total
        $this->total    =   $this->totalCollecting - $this->totalPayback;
    }
    
    /**
     * (non-PHPdoc)
     * @see modules/report/local/includes/AbsReportTemplate#renderReportHtml()
     */
    function renderReportHtml() {
        $this->_controller->assign('name', $this->name);
        $this->_controller->assign('date', $this->getDate());
        $this->_controller->assign('collectingIndex', 1);
        $this->_controller->assign('collectingReceiptList', $this->collectingReceiptList);
        $this->_controller->assign('totalCollecting', L10nHelper::renderNumber($this->totalCollecting));
        $this->_controller->assign('total', L10nHelper::renderNumber($this->total));
        
        /*$this->_controller->assign('paybackReceiptList', $this->paybackReceiptList);
        $this->_controller->assign('totalPayback', L10nHelper::renderNumber($this->totalPayback));*/
        

        /*$reportLogger = new PaymentLogger($this->_controller, $this->startDate, $this->endDate);
        $reportLogger->setReportType(PaymentLogger::REPORT_DAILY);
        $reportLogger->buildLogModel();
        $this->_controller->assign('loggerInfo', $reportLogger->renderLogInfo());*/
        
        if ($this->_print) {
            return $this->_controller->view->render('print.html');
        }
        return $this->_controller->view->render('view.html');
    }
}